package cn.iocoder.yudao.module.bk.service.publisher;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.bk.controller.admin.publisher.vo.*;
import cn.iocoder.yudao.module.bk.dal.dataobject.publisher.PublisherDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bk.dal.mysql.publisher.PublisherMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bk.enums.ErrorCodeConstants.*;

/**
 * 认证出版社 Service 实现类
 *
 * @author qq
 */
@Service
@Validated
public class PublisherServiceImpl implements PublisherService {

    @Resource
    private PublisherMapper publisherMapper;

    @Override
    public Long createPublisher(PublisherSaveReqVO createReqVO) {
        // 插入
        PublisherDO publisher = BeanUtils.toBean(createReqVO, PublisherDO.class);
        publisherMapper.insert(publisher);
        // 返回
        return publisher.getId();
    }

    @Override
    public void updatePublisher(PublisherSaveReqVO updateReqVO) {
        // 校验存在
        validatePublisherExists(updateReqVO.getId());
        // 更新
        PublisherDO updateObj = BeanUtils.toBean(updateReqVO, PublisherDO.class);
        publisherMapper.updateById(updateObj);
    }

    @Override
    public void deletePublisher(Long id) {
        // 校验存在
        validatePublisherExists(id);
        // 删除
        publisherMapper.deleteById(id);
    }

    private void validatePublisherExists(Long id) {
        if (publisherMapper.selectById(id) == null) {
            throw exception("PUBLISHER_NOT_EXISTS");
        }
    }

    @Override
    public PublisherDO getPublisher(Long id) {
        return publisherMapper.selectById(id);
    }

    @Override
    public PageResult<PublisherDO> getPublisherPage(PublisherPageReqVO pageReqVO) {
        return publisherMapper.selectPage(pageReqVO);
    }

}
